package com.wyg.blog.controller.manage_blog;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyg.blog.model.blog.Type;
import com.wyg.blog.service.blog.TypeService;
import com.wyg.blog.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: WYG
 * @time: 2020/6/30 20:29
 */
@RestController
@RequestMapping("/api/type")
public class TypeController {
    @Autowired
    TypeService typeService;

    @GetMapping("/getAllTypes")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<List<Type>> getAllTypes(){
        List<Type> types = typeService.selectAllTypes();
        return new JsonResult<>(types,"200","成功查询博客全部分类!");
    }

    @GetMapping("/getSomeTypes")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<IPage<Type>> getSomeTypes(@RequestParam("current") Integer current, @RequestParam("size") Integer size){
        Page<Type> page = new Page<>(current,size);
        IPage<Type> typeIPage = typeService.selectSomeTypes(page);
        return new JsonResult<>(typeIPage,"200","分页查询博客全部分类!");
    }

    @GetMapping("/getType")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Type> getType(@RequestParam("name") String name){
        Type type = typeService.selectTypeByName(name);
        return new JsonResult<>(type,"200","分类详细信息!");
    }

    @PostMapping("/addType")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Integer> addType(@RequestBody Map<String,String> typeInfo){
        Type type = new Type();
        type.setId(0);
        type.setType(typeInfo.get("typeName"));
        type.setCreate_time(new Date());
        type.setState(true);
        boolean res = typeService.insertType(type);
        if (res) {
           return new JsonResult<>(1,"200","添加成功!");
        } else {
            return new JsonResult<>(0,"901","添加失败!");
        }
    }

    @PostMapping("/updateType")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Integer> updateType(@RequestBody Map<String,String> typeInfo){
        boolean res = typeService.renewType(Integer.parseInt(typeInfo.get("id")),typeInfo.get("type"));
        if (res) {
            return new JsonResult<>(1,"200","修改成功!");
        } else {
            return new JsonResult<>(0,"902","修改失败!");
        }
    }

    @PostMapping("/updateTypeState")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Integer> updateTypeState(@RequestBody Map<String,Integer> typeInfo){
        Type type = typeService.selectTypeById(typeInfo.get("type_id"));
        boolean res = typeService.renewTypeState(typeInfo.get("type_id"), !type.isState());
        if (res) {
            return new JsonResult<>(1,"200","修改成功!");
        } else {
            return new JsonResult<>(0,"902","修改失败!");
        }
    }

    @PostMapping("/deleteType")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Integer> deleteType(@RequestParam("type_id") Integer id){
        boolean res = typeService.removeType(id);
        if (res) {
            return new JsonResult<>(1,"200","删除成功!");
        } else {
            return new JsonResult<>(0,"903","删除失败!");
        }
    }

    @GetMapping("fuzzy")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPER_ADMIN')")
    public JsonResult<List<Type>> fuzzySearch(@RequestParam("type") String type) {
        String param = "%" + type + "%";
        List<Type> types = typeService.fuzzySearch(param);
        return new JsonResult<>(types, "200", "标签信息模糊查询成功!");
    }

}
